<?php namespace Phpcmf\Controllers\Member;

class Shenhe extends \Phpcmf\Member\Module
{

	// 内容审核
	public function index() {
		
		// 获取get参数
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$cid = (int)\Phpcmf\Service::L('input')->get('cid');
		$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize'); // 页数量
		$page = (int)\Phpcmf\Service::L('input')->get('page'); // 当前页

		if($id){
			$id_data = $id;
		} else {
			$id_data = $cid;
		}
		
		if(!$pagesize){
			$pagesize = 20; // 默认值
		}
		if(!$page){
			$page = 1; // 默认第一页
		}
		if($cid){
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$shuju_data = \Phpcmf\Service::M()->table('1_zhishi_form_kecheng')->where('status', 0)->where('cid', $id_data)->order_by('id', 'desc')->limit($per_page, $offset)->getAll();
		} else {
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$shuju_data = \Phpcmf\Service::M()->table('1_zhishi_form_kecheng')->where('status', 0)->order_by('id', 'desc')->limit($per_page, $offset)->getAll();
		}

		// 循环 $shuju_data ， 把 $shuju_data['content'] 中的图片路径替换为 域名 + 图片路径
		$list_data = [];
		foreach ($shuju_data as $key => $val) {
			$quan_data = \Phpcmf\Service::M()->db->table('1_zhishi')->where('id', $val['cid'])->where('fstatus', 1)->get()->getRowArray();
			// 内容图片列表
			$tupian_data = dr_get_content_img($val['neirong']);
			$tupian_list = [];
			foreach ($tupian_data as $k => $v) {
				// 去除&quot;及前面的内容
                $v = preg_replace('/.*&quot;/', '', $v);
				$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
				
			}
			// 头像
			if(dr_avatar($val['uid'])){
				$touxiang = dr_avatar($val['uid']);
			} else {
				$touxiang = 1;
			}
			// 九个图 
			$jiugetu_list = [];
			foreach ($val['jiugetu'] as $k => $v) {
				if ($k <= 8) {
					$jiugetu_list[] = dr_thumb($v['file'], 250, 250, 0, 'crop');
				}
			}
			// 名称
			$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
			$user = dr_member_info($val['uid']);
			if($userweixin){
				$mingcheng = $userweixin['nickname'];
			} else if($user['name']) {
				$mingcheng = $user['name'];
			} else {
				$mingcheng = $user['username'];
			}
			$dianzan = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng_dianzan')->where('tieid', $val['id'])->countAllResults();
			$shoucang = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng_shoucang')->where('tieid', $val['id'])->countAllResults();
			//$pinglun = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng_comment')->where('cid', $val['id'])->where(' status', 1)->countAllResults();
			// 点赞用户头像
			$dianzan_data = \Phpcmf\Service::M()->table('1_zhishi_form_kecheng_dianzan')->where('tieid', $val['id'])->getAll();
			$dianzan_touxiang = [];
			foreach ($dianzan_data as $k => $v) {
				$dianzan_touxiang[] = dr_avatar($v['uid']);
			}
			
			// 帖子内容列表
			$list_data[] = [
				'id' => $val['id'],
				'cid' => $val['cid'],
				'uid' => $val['uid'],
				'touxiang' => $touxiang,
				'mingcheng' => $mingcheng,
				'title' => $val['title'],
				'tupian' => $tupian_list,
				'jiugetu' => $jiugetu_list,
				'jianjie' => dr_strcut(dr_clearhtml($val['neirong']), 100),
				'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
				'quanzi' => $quan_data['title'],
				'dianzan' => $dianzan,
				'pinglun' => $val['comments'],
				'shoucang' => $shoucang,
				'dianzan_touxiang' => $dianzan_touxiang,
			];
		}

		$this->_json('1', '读取成功', $list_data);
		
	}
	// 审核
	public function tong() {
		//get 参数
		if(!$this->member['is_admin']){
			$this->_json('0', '您没有权限操作');
		}
		$id = (int)\Phpcmf\Service::L('input')->get('id'); //帖子id
		
		// 审核
		\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->update(['status' => 1]);
		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->get()->getRowArray();
		// 通知用户
		$dataduixin = [
			'catid'=> 19, //类型
			'title' => '帖子审核通过', //标题
			'zhiding' => '', //置顶
			'content'=> dr_strcut(dr_clearhtml($shuju_data['neirong']), 30), //通知内容
			'uid'=> $this->uid, //发布人的uid
			'faxinren'=> $this->uid, //发布人的uid
			'shouxinren'=> (int)$shuju_data['uid'],//收信人
			'tupian'=> '', //图片
			'shipin'=> '', //视频
			'yuyin'=> '', //语音
			'fujian'=> '', //附件
			'yidu'=> 0, //已读
			'quanziid'=> $shuju_data['cid'], //圈子ID
			'quantieid'=> $id, //圈帖ID
			'url' => '',
		];
		if($this->uid != (int)$shuju_data['uid']){
			// 通知
			\Phpcmf\Service::M('auth', 'duixin')->addtongzhi($dataduixin);
		}
		$this->_json('1', '操作成功');
	}
	// 拒绝
	public function jujue() {
		// 检查权限
		if(!$this->member['is_admin']){
			$this->_json('0', '您没有权限操作');
		}
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id'); //帖子id
		// 拒绝
		\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->update(['status' => 2]);
		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->get()->getRowArray();
		// 通知用户
		$dataduixin = [
			'catid'=> 19, //类型
			'title' => '帖子被拒绝退回', //标题
			'zhiding' => '', //置顶
			'content'=> dr_strcut(dr_clearhtml($shuju_data['neirong']), 30), //通知内容
			'uid'=> $this->uid, //发布人的uid
			'faxinren'=> $this->uid, //发布人的uid
			'shouxinren'=> (int)$shuju_data['uid'],//收信人
			'tupian'=> '', //图片
			'shipin'=> '', //视频
			'yuyin'=> '', //语音
			'fujian'=> '', //附件
			'yidu'=> 0, //已读
			'quanziid'=> $shuju_data['cid'], //圈子ID
			'quantieid'=> $id, //圈帖ID
			'url' => '',
		];
		if($this->uid != (int)$shuju_data['uid']){
			// 通知
			\Phpcmf\Service::M('auth', 'duixin')->addtongzhi($dataduixin);
		}
		$this->_json('1', '操作成功');
	}
    
	// 知识审核数量
	public function shenhe_count() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$cid = (int)\Phpcmf\Service::L('input')->get('cid');
		if($id){
			$id_data = $id;
		} else {
			$id_data = $cid;
		}
		
		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi')->where('id', $id_data)->get()->getRowArray();
		// 审核数量
		if($this->member['is_admin'] || $this->uid == (int)$shuju_data['uid']){
			if(!$id && !$cid) {
				$count = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('status', 0)->countAllResults();

			} else {
				$count = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('status', 0)->where('cid', $id_data)->countAllResults();

			}
			
		} else {
			$count = 0;
		}
		$this->_json('1', '读取成功', $count);
	}
	// 知识审核数量
	public function shenhe_zhishi_shu() {

		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi')->where('uid', $this->uid)->where('fstatus', 2)->countAllResults();
		$data_list_0 = \Phpcmf\Service::M()->table('1_zhishi')->where('uid', $this->uid)->where('fstatus', 2)->getAll();
		//循环
        $data_list_0 = $data_list_0 ?: [];
        foreach($data_list_0 as $key => $val){
            $data_list_0[$key]['title'] = $val['title'];
            $data_list_0[$key]['content'] = dr_string2array($val['content']);
            // 修复字符串偏移访问错误：先确保content是数组格式
            $content_data = is_string($val['content']) ? json_decode($val['content'], true) : $val['content'];
            $content_data = is_array($content_data) ? $content_data : [];
            $data_list_0[$key]['content']['inputtime'] = date('Y-m-d H:i', $content_data['inputtime'] ?? time());
        }
		// 审核数量
		$data = [
			'shuliang' => $shuju_data,
			'list' => $data_list_0,
		];
		
		$this->_json('1', '读取成功', $data);
	}
}
